
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <style>
        .el-table .warning-row {
            background: oldlace;
        }
        .el-table .success-row {
            background: #f0f9eb;
        }
    </style>
</head>
<body>
<script src="js/vue.js"></script>
<script src="js/axios.js"></script>
<script src="element-ui/lib-master/index.js"></script>
<link rel="stylesheet" href="element-ui/lib-master/theme-chalk/index.css">
<div id="app">
    <!--搜索表单-->
    <el-form :inline="true" :model="brand" class="demo-form-inline"  ref="brandForm">
        <el-form-item label="当前状态">
            <el-select v-model="brand.status" placeholder="当前状态">
                <el-option label="启用" value=1></el-option>
                <el-option label="禁用" value=0></el-option>
            </el-select>
        </el-form-item>

        <el-form-item label="企业名称">
            <el-input v-model="brand.companyName" placeholder="企业名称"></el-input>
        </el-form-item>

        <el-form-item label="品牌名称">
            <el-input v-model="brand.brandName" placeholder="品牌名称"></el-input>
        </el-form-item>

        <el-form-item>
            <el-button type="primary" @click="onSubmit">查询</el-button>
        </el-form-item>
    </el-form>

    <!--按钮-->
    <el-row>
        <el-button type="danger" plain @click="deletebyids">批量删除</el-button>
        <el-button type="primary" plain @click="dialogVisible = true">新增</el-button>
    </el-row>

    <!--添加数据对话框表单-->
    <el-dialog
        title="编辑品牌"
        :visible.sync="dialogVisible"
        width="30%">
    <el-form ref="form" :model="brand" label-width="80px">
        <el-form-item label="品牌名称">
            <el-input v-model="brand.brandName"></el-input>
        </el-form-item>

        <el-form-item label="企业名称">
            <el-input v-model="brand.companyName"></el-input>
        </el-form-item>

        <el-form-item label="排序">
            <el-input v-model="brand.ordered"></el-input>
        </el-form-item>

        <el-form-item label="备注">
            <el-input type="textarea" v-model="brand.description"></el-input>
        </el-form-item>

        <el-form-item label="状态">
            <el-switch v-model="brand.status"
                       active-value="1"
                       inactive-value="0"
            ></el-switch>
        </el-form-item>
        <el-form-item>
            <el-button type="primary" @click="addBrand">提交</el-button>
            <el-button @click=" dialogVisible = false">取消</el-button>
        </el-form-item>
    </el-form>
</el-dialog>
    <!--表格-->
    <template>
        <el-table
                :data="tableData"
                style="width: 100%"
                :row-class-name="tableRowClassName"
                @selection-change="handleSelectionChange">

            <el-table-column
                    type="selection"
                    width="55">
            </el-table-column>

            <el-table-column
                    prop="id"
                    label="ID"
                    type="index"
                    width="50">
            </el-table-column>

            <el-table-column
                    prop="brandName"
                    label="品牌名称"
                    align="center">
            </el-table-column>

            <el-table-column
                    prop="companyName"
                    label="企业名称"
                    align="center">
            </el-table-column>

            <el-table-column
                    prop="ordered"
                    label="排序"
                    align="center">
            </el-table-column>

            <el-table-column
                    prop="statusStr"
                    label="当前状态"
                    align="center">
            </el-table-column>

            <el-table-column
                    align="center"
                    label="操作">
                <template slot-scope="scope">
                    <el-row>
                        <el-button type="primary" @click="editBrand(scope.row)">修改</el-button>
                        <el-button type="danger" @click="deleteBrand(scope.row.id)">删除</el-button>
                    </el-row>
                </template>
            </el-table-column>

        </el-table>
    </template>

    <!--分页工具条-->
    <el-pagination
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
            :current-page="currentPage"
            :page-sizes="[5, 10, 15, 20]"
            :page-size="5"
            layout="total, sizes, prev, pager, next, jumper"
            :total="totalcount">
    </el-pagination>

</div>
<script>
    new Vue({
        el: "#app",
            mounted() {
                // 监听点击事件
                document.addEventListener("click", this.handleClickOutside);
        this.selectAll();
           },
        methods: {
            handleClickOutside(event) {
                // 检查点击的位置是否在form外部，如果是，清空表单
                if (!this.$refs.brandForm.contains(event.target)) {
                   this.resetBrandData();
                }
            },
            deletebyids()
            {

                for(var i = 0; i < this.multipleSelection.length; i++)
                {
                    this.selectedids[i] = this.multipleSelection[i].id;
                }
                axios.post("http://localhost:8080/vue-element/brand/deletebyids", this.selectedids, {
                    headers: {
                        'Content-Type': 'application/json;charset=UTF-8'
                    }
                }).then(response=>
                {
                    if (response.data === "success") {
                        this.$message({
                            message: '删除成功',
                            type: 'success'
                        });
                        this.selectAll();  // 重新加载数据
                    }
                }).catch((error) => {
                    console.error('删除失败:', error);  // 打印详细的错误信息
                    this.$message({
                        message: `删除失败: ${error.response ? error.response.data : error.message}`,
                        type: 'error'
                    });
                });
            },
            editBrand(row) {
                this.brand = row;
                this.brand.status = String(this.brand.status);
                this.dialogVisible = true;
            },
            tableRowClassName({row, rowIndex}) {
                if (rowIndex === 1) {
                    return 'warning-row';
                } else if (rowIndex === 3) {
                    return 'success-row';
                }
                return '';
            },
            // 复选框选中后执行的方法
            handleSelectionChange(val) {
                this.multipleSelection = val;

                console.log(this.multipleSelection)
            },
            deleteBrand(id) {
                 // 获取对应的 `this`
                axios.get("http://localhost:8080/vue-element/brand/delete?id=" + id).then(response=> {
                    if (response.data === "success") {
                       this.$message({
                            message: '删除成功',
                            type: 'success'
                        });
                        this.selectAll();  // 重新加载数据
                    } else {
                        this.$message({
                            message: '删除失败',
                            type: 'error'
                        });
                    }
                }).catch(function (error) {
                    this.$message({
                        message: '请求错误',
                        type: 'error'
                    });
                });
            },
            selectAll() {
                axios.post("http://localhost:8080/vue-element/brand/selectbycondition?page=" + this.currentPage + "&size=" + this.pageSize, this.brand, {
                    headers: {
                        'Content-Type': 'application/json;charset=UTF-8'
                    }
                }).then(response=> {
                    //设置页面数据
                    this.tableData = response.data.rows;
                    this.totalcount = response.data.totalpage;
                });
            },
            // 查询方法
            onSubmit() {
                this.selectAll();
            },


            // 添加数据
            addBrand() {

                // 发送 axios 请求
                if (this.brand.id) {
                    // 更新操作
                    axios.post("http://localhost:8080/vue-element/brand/update", this.brand, {
                        headers: {
                            'Content-Type': 'application/json;charset=UTF-8'
                        }
                    }).then((response) => {
                        if (response.data === "success") {
                            this.dialogVisible = false;  // 关闭对话框
                            this.$message({
                                message: '修改成功',
                                type: 'success'
                            });
                        }
                    }).catch((error) => {
                        console.error('修改失败:', error);  // 打印详细的错误信息
                        this.$message({
                            message: `修改失败: ${error.response ? error.response.data : error.message}`,
                            type: 'error'
                        });
                    });
                } else {
                    // 新增操作
                    axios.post("http://localhost:8080/vue-element/brand/add", this.brand, {
                        headers: {
                            'Content-Type': 'application/json;charset=UTF-8'
                        }
                    }).then((response) => {
                        if (response.data === "success") {
                            this.dialogVisible = false;  // 关闭对话框
                            this.$message({
                                message: '添加成功',
                                type: 'success'
                            });
                        }
                    }).catch((error) => {
                        console.error('添加失败:', error);  // 打印详细的错误信息
                        this.$message({
                            message: `添加失败: ${error.response ? error.response.data : error.message}`,
                            type: 'error'
                        });
                    });
                }
            },
            resetBrandData() {
                this.brand = {
                    status: '',
                    brandName: '',
                    companyName: '',
                    id: "",
                    ordered: "",
                    description: ""
                };
            },
            //分页
            handleSizeChange(val) {
                this.pageSize = val;
                this.selectAll();
            },
            handleCurrentChange(val) {
                this.currentPage = val;
                this.selectAll();
            }
        },
        watch: {
            dialogVisible(newVal) {
                if (!newVal) {  // 当 dialogVisible 为 false 时
                    this.resetBrandData();  // 清空表单数据
                    this.selectAll();  // 重新加载数据
                }
            }
        },
        data() {
            return {
                // 当前页码
                totalcount: 10,
                currentPage: 1,
                pageSize: 5,
                // 添加数据对话框是否展示的标记
                dialogVisible: false,

                // 品牌模型数据
                brand: {
                    status:'',
                    brandName: '',
                    companyName: '',
                    id:"",
                    ordered:"",
                    description:""
                },
                // 复选框选中数据集合
                selectedids:[],
                multipleSelection: [],
                // 表格数据
                tableData: [{
                    brandName: '华为',
                    companyName: '华为科技有限公司',
                    ordered: '100',
                    status: "1"
                }, {
                    brandName: '华为',
                    companyName: '华为科技有限公司',
                    ordered: '100',
                    status: "1"
                }, {
                    brandName: '华为',
                    companyName: '华为科技有限公司',
                    ordered: '100',
                    status: "1"
                }, {
                    brandName: '华为',
                    companyName: '华为科技有限公司',
                    ordered: '100',
                    status: "1"
                }]
            }
        }
    })
</script>
</body>
</html>
